package com.mingyizhudao.app.view.mine;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.utils.Utils;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.igexin.sdk.PushManager;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.view.MYApplication;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.mine.adpter.OrderItemCommonAdpter;
import com.mingyizhudao.app.view.mine.adpter.StatusListAdapter;
import com.mingyizhudao.app.view.mine.bean.BookingStatusEntity;
import com.mingyizhudao.app.view.mine.bean.OrderItemEntity;
import com.mingyizhudao.app.view.mine.bean.BookingStatusEntity.StatusEntity;

public class OrderListCommonActivity extends MYBaseActivity{
	private ListView listView;
	private OrderItemCommonAdpter adpter;
	
	private List<StatusEntity> status;
	private String type;
	private String title ="全部";
	
	private LinearLayout ll_data_null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_list);
		initView();
		
	}
	
	private void initView(){
		activity = this;
		type = getIntent().getStringExtra("type");
		title = getIntent().getStringExtra("title");
		
		ll_data_null = (LinearLayout) findViewById(R.id.ll_data_null);
		listView = (ListView)findViewById(R.id.lv_order);
		adpter = new OrderItemCommonAdpter(this);
		listView.setOnItemClickListener(onItemClickListener);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		doNet();
	}

	@Override
	public void initHeaderView() {
		setTopViewBg(R.color.actionbar_bg_color);
		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
        setTitleViewValue(title, R.color.white, R.drawable.go_down_order, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (status!=null&&status.size()>0) {
					initDiseasePopupWindow(v, activity);
				}else {
					Toast.makeText( activity, "数据加载中！", 0 ).show();
				}
				
			}
		});
	}
	
    public void doNet(){
    	LoadingDialog.getInstance(activity).show();
    	
    	String urlStatus = Constants.baseURL + "bookingstatus";
		getHttpResult(urlStatus, Constants.HTTP_INDEX.Second, new TypeToken<QjResult<BookingStatusEntity>>() {});
    	
    	
    	String tokenParam = CommonUtils.getTokenParam(activity);
        if (type!=null) {
        	tokenParam = tokenParam + "&bk_status="+type;
		}
    	
        String url = CommonUtils.getBookingListUrl( activity )+tokenParam;
    	getHttpResult(url, Constants.HTTP_INDEX.First, new TypeToken<QjResult<List<OrderItemEntity>>>() {});
    }
    
    @Override
    public void onSuccess(QjResult result, String flg) {
    	super.onSuccess(result, flg);
    	if (flg.equals(Constants.HTTP_INDEX.First)) {
    		setGeTuiAlias();
    		
    		List<OrderItemEntity> list = (List<OrderItemEntity>)result.getResults();
    		if(list != null && list.size() > 0){
    			ll_data_null.setVisibility(View.GONE);
    			adpter.clear();
    			adpter.addAll(list);
    			listView.setAdapter(adpter);
    			return;
    		}else{
    			adpter.clear();
				listView.setAdapter(adpter);
				ll_data_null.setVisibility(View.VISIBLE);
    			Toast.makeText( activity, "暂时没有数据！", 0 ).show();	
    		}
		}else if (flg.equals(Constants.HTTP_INDEX.Second)) {
			
			BookingStatusEntity bookingStatusEntity = (BookingStatusEntity) result.getResults();
			if (bookingStatusEntity!=null) {
				status = bookingStatusEntity.getStatus();
			}
			
		}
		
			
    }
    
    @Override
    public void onCompleted(Exception e, String flg) {
    	super.onCompleted(e, flg);
    	LoadingDialog.getInstance(activity).dismiss();
    }
    
    private OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {	
//			OrderItemEntity item = adpter.getItem(arg2);
//			IntentHelper.getInstance(activity).gotoActivityWithURL(OrderStateCommonActivity.class, item.getActionUrl());
			
			OrderItemEntity item = adpter.getItem(arg2);
			Bundle bundle = new Bundle();
			bundle.putSerializable( Constants.PARM_ACTION_URL, item.getActionUrl());
			bundle.putSerializable("orderItemEntity", item);
			
            if ("6".equals(item.getBk_status())) {
				IntentHelper.getInstance(activity).gotoActivity(CommentSubmitActivity.class, bundle);
				
			}else if ("8".equals(item.getBk_status())) {
				IntentHelper.getInstance(activity).gotoActivity(CommentWatchActivity.class, bundle);
				
			}else {
				IntentHelper.getInstance(activity).gotoActivity(OrderStateCommonActivity.class, bundle);
			}
		}
	};
	

	
	private void initDiseasePopupWindow(View v, Context context) {
		// 设置pop窗口的视图，用布局转换器将xml转换成view
		View contentView =  LayoutInflater.from(this).inflate(R.layout.pop_dept, null);
		// 获得pop窗口，设置大小(宽高设置为填充)，ViewGroup.LayoutParams.MATCH_PARENT填充父类
		final PopupWindow pop = new PopupWindow(
				contentView,
				LayoutParams.MATCH_PARENT,
				(int) (MYApplication.heightPixels - (MYApplication.startheight + ((43 ) * MYApplication.density))));
		ListView lv_dept = (ListView) contentView.findViewById(R.id.lv_dept);

		StatusListAdapter cityListAdapter = new StatusListAdapter(context,status);
		
		lv_dept.setAdapter(cityListAdapter);
		// 获得焦点
		pop.setFocusable(true);
		pop.setBackgroundDrawable(new BitmapDrawable());// 点击完按钮后另其消失（方法：设置一个空的bitmap）
		// 设置pop窗口显示的位置
		// 将按钮和pop窗口关联上，v表示一个显示的基点(按钮的基点为左下点)
		pop.showAtLocation(
				v,
				Gravity.TOP,
				0,
				(int) (MYApplication.startheight + (43 * MYApplication.density)));
		pop.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
			}
		});

		lv_dept.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				
				title = status.get(position).getStatus_name();
				setTitleViewValue(title);

				type = "" + status.get(position).getBk_status();
				
				
				LoadingDialog.getInstance(activity).show();

				String tokenParam = CommonUtils.getTokenParam(activity);
		        if (type!=null) {
		        	tokenParam = tokenParam + "&bk_status="+type;
				}
		        String url = CommonUtils.getBookingListUrl( activity )+tokenParam;
		    	getHttpResult(url, Constants.HTTP_INDEX.First, new TypeToken<QjResult<List<OrderItemEntity>>>() {});
				
				
				if (pop.isShowing()) {
					pop.dismiss();
				}
			}

		});

	}
	
	
	
	private void setGeTuiAlias() {
		String mobile =CommonUtils.getMobile( activity );
	    String alias = "m_"+mobile;
	    
	    alias = Utils.Md5String(alias);
	    
	    if (TextUtils.isEmpty(alias)) {
	        //Toast.makeText(activity,"别名不能为空！", Toast.LENGTH_SHORT).show();
	        return;
	    }
	    if (!CommonUtils.isValidTagAndAlias(alias)) {
	        //Toast.makeText(activity,"无效的别名！", Toast.LENGTH_SHORT).show();
	        return;
	    }

	    // 设置别名
	    
	    PushManager.getInstance().bindAlias(activity, alias);
	   }
}
